<?php

namespace app\api\controller;

use app\admin\model\Order;
use app\common\controller\Api;
use app\common\model\Banner;
use think\Db;
use fast\Http;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }
//    /**
//     * 首页
//     *
//     */


    public function delHua()
    {
        $key = $this->request->Post('key');
        if(empty($key)){
            $this->error('请登录');
        }

        $phone = $this->request->Post('phone');
        if(empty($phone)){
            $this->error('请登录');
        }

        $phonekey=$phone.'liaoyuziwei123';
        if($key !== md5($phonekey)){
            $this->error('请登录');
        }

        $id = $this->request->post('id');

        $arr = Order::where(['id'=>$id,'phone'=>$phone])->delete();
        if($arr){
            $this->success('删除成功');
        }

        $this->error('删除失败');
    }

  public function telLogin()
    {
        $token = $this->getAccessToken();

       $data['code'] =   $this->request->get('code');
       
      
        $url2 = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=$token";

        $res2 = Http::post($url2,json_encode($data));

      
        return $this->success('',json_decode($res2));
    }

    public function getAccessToken()
    {
        $url='https://api.weixin.qq.com/cgi-bin/token';

        $data = [
            'grant_type'=>'client_credential',
            'appid'=>'wx7fa4b1b9058ce815',
            'secret'    => '675f3ba95245aaecc9e49cf54b4d62b0',
        ]    ;

        $res = Http::get($url,$data);
        $arr = json_decode($res);
        $access_token = $arr->access_token;
        return  $access_token;
    }
    public function banner()
    {
       $code = $this->request->Post('code');
       
       $data = str_replace('"', '\\"', $code);
       $code = json_encode($code);
    //   var_dump($data);
       
//  if($data == "ziweichapan"){
     
     $list = [
         'type' =>2,
         'src' =>'https://zw.ulovemall.cn/h5/ziwei/#/pages/my/index',
         'code'=>'ziweichapan',
        
         ];
    
     return $this->success('',$list);
 //}

        $list = \app\admin\model\Banner::field('id,img,type,src,code,name')->find();

        return $this->success('',$list);
    }

    public function order()
    {
        $key = $this->request->Post('key');
        if(empty($key)){
            $this->error('请登录');
        }

        $phone = $this->request->Post('phone');
        if(empty($phone)){
            $this->error('请登录');
        }

        $phonekey=$phone.'liaoyuziwei123';
       if($key !== md5($phonekey)){
           $this->error('请登录');
       }

        $date = $this->request->Post('date');
        $name = $this->request->Post('name');
        $gender = $this->request->Post('gender');
        $type = $this->request->Post('type');
        $lunarDate = $this->request->Post('lunarDate');
        $solarDate = $this->request->Post('solarDate');
        $mainStar = $this->request->Post('mainStar');
        $time = $this->request->Post('time');
        $phone = $this->request->Post('phone');

        if(empty($name)){
            $this->error('姓名不为空');
        }
        if(empty($date)){
            $this->error('生日不为空');
        }

        Db::startTrans();
        try {

            $arr =new Order();
            $arr->date = $date;
            $arr->name = $name;
            $arr->gender = $gender;
            $arr->type = $type;
            $arr->lunarDate = $lunarDate;
            $arr->solarDate = $solarDate;
            $arr->mainStar = $mainStar;
            $arr->time = $time;
            $arr->phone =$phone;
            $arr->order_id ='P'.date("YmdHis") . mt_rand(100000, 999999);

            $arr->save();

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        return $this->success('提交成功');
    }

    public function orderList()
    {
        $key = $this->request->Post('key');
        if(empty($key)){
            $this->error('请登录');
        }

        $phone = $this->request->Post('phone');
        if(empty($phone)){
            $this->error('请登录');
        }

        $phonekey=$phone.'liaoyuziwei123';
        if($key !== md5($phonekey)){
            $this->error('请登录');
        }
        // 筛选条件
        $where = [
            'phone' => $phone
        ];
        $search = $this->request->get('search');
        if (!empty($search)) {
            $where['name'] = ['like', '%' . $search . '%'];
        }


        $list = Order::where($where)->order('id desc')->select();
//        print_r($list);
//        exit('44');
        return $this->success($list);
    }

}
